Conditions | 1 |
Paths | 2 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
15 | function upgrades(state, visibility, upgrade, data) { |
||
16 | let ct = this; |
||
17 | ct.state = state; |
||
18 | ct.data = data; |
||
19 | let sortFunc = [ |
||
20 | function(a,b){ |
||
21 | if(data.upgrades[a].name === data.upgrades[b].name){ |
||
22 | return data.upgrades[a].price - data.upgrades[b].price; |
||
23 | } |
||
24 | if(data.upgrades[a].name < data.upgrades[b].name){ |
||
25 | return -1; |
||
26 | }else{ |
||
27 | return 1; |
||
28 | } |
||
29 | }, |
||
30 | (a,b) => data.upgrades[a].price - data.upgrades[b].price |
||
31 | ]; |
||
32 | |||
33 | // tries to buy all the upgrades it can, starting from the cheapest |
||
34 | ct.buyAll = function (slot) { |
||
35 | let currency = data.elements[slot.element].main; |
||
36 | let cheapest; |
||
37 | let cheapestPrice; |
||
38 | do{ |
||
39 | cheapest = null; |
||
40 | cheapestPrice = Infinity; |
||
|
|||
41 | for(let up of ct.visibleUpgrades(slot, data.upgrades)){ |
||
42 | let price = data.upgrades[up].price; |
||
43 | if(!slot.upgrades[up] && |
||
44 | price <= state.player.resources[currency].number){ |
||
45 | if(price < cheapestPrice){ |
||
46 | cheapest = up; |
||
47 | cheapestPrice = price; |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | if(cheapest){ |
||
52 | upgrade.buyUpgrade(state.player, |
||
53 | slot.upgrades, |
||
54 | data.upgrades[cheapest], |
||
55 | cheapest, |
||
56 | cheapestPrice, |
||
57 | currency); |
||
58 | } |
||
59 | }while(cheapest); |
||
60 | }; |
||
61 | |||
62 | ct.buyUpgrade = function (name, slot) { |
||
63 | let price = data.upgrades[name].price; |
||
64 | let currency = data.elements[slot.element].main; |
||
65 | upgrade.buyUpgrade(state.player, |
||
66 | slot.upgrades, |
||
67 | data.upgrades[name], |
||
68 | name, |
||
69 | price, |
||
70 | currency); |
||
71 | }; |
||
72 | |||
73 | ct.visibleUpgrades = function(slot) { |
||
74 | return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[state.player.options.sortIndex]); |
||
75 | }; |
||
76 | |||
77 | function isBasicUpgradeVisible(name, slot) { |
||
78 | let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name]); |
||
79 | return isVisible && (!state.player.options.hideBought || !slot.upgrades[name]); |
||
80 | } |
||
81 | } |
||
82 |